﻿using System;
using System.Collections.Generic;
using System.Threading;

public class Obstacle
{
    private int x;
    private int y;
    private char element;
    ConsoleColor color;

    public Obstacle()
    {
    }

    public Obstacle(int x, int y, char element)
    {
        this.x = x;
        this.y = y;
        this.element = element;
    }

    public int X
    {
        get
        {
            return this.x;
        }

        set
        {
            this.x = value;
        }
    }

    public int Y
    {
        get
        {
            return this.y;
        }

        set
        {
            this.y = value;
        }
    }

    public char Element
    {
        get
        {
            return this.element;
        }

        set
        {
            this.element = value;
        }
    }

    public ConsoleColor Color
    {
        get
        {
            return this.color;
        }

        set
        {
            this.color = value;
        }
    }
}

class FallingRocks
{
    static char[] obstaclesCharactersToDisplay = { '^', '@', '*', '&', '+', '%', '$', '#', '!', '.' };
    static List<Obstacle> rocks = new List<Obstacle>();
    static int playerCoordinateX = Console.WindowWidth / 2;
    static bool collision = false;
    static Random randomObstaclesPos = new Random();
    static Random random = new Random();
    static ConsoleColor[] colors = { ConsoleColor.Blue, ConsoleColor.Cyan, ConsoleColor.DarkBlue, ConsoleColor.DarkCyan,
                                       ConsoleColor.DarkGray, ConsoleColor.DarkRed, ConsoleColor.Magenta,
                                       ConsoleColor.Yellow, ConsoleColor.White };
    static int score = 0;  

    static Obstacle ObstaclesGenerator()
    {
        Obstacle rock = new Obstacle();                
        rock.X = randomObstaclesPos.Next(Console.WindowWidth);
        rock.Y = 0;
        rock.Element = obstaclesCharactersToDisplay[randomObstaclesPos.Next(obstaclesCharactersToDisplay.Length)];
        rock.Color = colors[random.Next(colors.Length)];
        return rock;
    }

    static void DrawObstacles()
    {
        for (int i = 0; i < rocks.Count; i++)
        {
            Console.SetCursorPosition(rocks[i].X, rocks[i].Y);
            Console.ForegroundColor = rocks[i].Color;
            Console.Write(rocks[i].Element);
            if (rocks[i].Y < Console.WindowHeight - 1)
            {
                rocks[i].Y += 1;  
            }
            else
            {
                CollisionCheck(rocks[i]);
                rocks.RemoveAt(i);
                score++;
            }
            Console.ResetColor();
        }
    }

    private static void DrawScore()
    {
        Console.SetCursorPosition(Console.WindowWidth/2-10,1);
        Console.ForegroundColor = ConsoleColor.White;
        Console.Write("Your score is:{0}",score);
        Console.ResetColor();
    }

    private static void CollisionCheck(Obstacle rock)
    {
        if (rock.X == playerCoordinateX)
        {
            Console.WriteLine("Game over!!!");
            collision = true;
        }
    }

    static void Player()
    {
        if (Console.KeyAvailable)
        {
            ConsoleKeyInfo keyInfo = Console.ReadKey();
            if (keyInfo.Key == ConsoleKey.LeftArrow)
            {
                MoveLeft();
            }
            if (keyInfo.Key == ConsoleKey.RightArrow)
            {
                MoveRight();
            }
        }
        Console.SetCursorPosition(playerCoordinateX, Console.WindowHeight - 1);
        Console.ForegroundColor = ConsoleColor.Red;
        Console.Write('0');
        Console.ResetColor();
    }
  
    private static void MoveRight()
    {
        if (playerCoordinateX != Console.WindowWidth - 1)
        {
            playerCoordinateX += 1;  
        }
    }

    private static void MoveLeft()
    {
        if (playerCoordinateX != 0)
        {
            playerCoordinateX -= 1;
        }
    }

    static void Main()
    {
        while (true)
        {
            Console.Clear();
            Console.WindowWidth = 50;
            Console.BufferHeight = Console.WindowHeight;
            Console.BufferWidth = Console.WindowWidth;
            rocks.Add(ObstaclesGenerator());
            DrawObstacles();
            DrawScore();
            Player();
            if (collision)
            {
                break;
            }
            Thread.Sleep(150);            
        }
    }
  
    
}